package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.JobOverviewAdapter;
import com.kupurui.asteacher.bean.JobOverviewBean;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.ui.index.StudentDatailsAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 作业概览
 * Created by Administrator on 2017/7/24.
 */

public class JobOverviewAty extends BaseAty {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.gridview)
    GridView gridview;
    @Bind(R.id.tv_work_title)
    TextView tvWorkTitle;
    @Bind(R.id.tv_finish_time)
    TextView tvFinishTime;
    @Bind(R.id.tv_right_percent)
    TextView tvRightPercent;
    @Bind(R.id.tv_finish)
    TextView tvFinish;
    @Bind(R.id.tv_save_num)
    TextView tvSaveNum;
    @Bind(R.id.tv_subject_num)
    TextView tvSubjectNum;


    JobOverviewBean jobOverviewBean;
    List<JobOverviewBean.AnswerBean> list;
    JobOverviewAdapter adapter;

    private String work_id = "";//作业ID
    private String class_id = "";//班级ID

    Teacher teacher;

    @Override
    public int getLayoutId() {
        return R.layout.job_overview_aty;
    }

    @Override
    public void initData() {
        teacher = new Teacher();
        if (getIntent().getExtras() != null){
            work_id = getIntent().getStringExtra("id");
            class_id = getIntent().getStringExtra("class_id");
        }

        list = new ArrayList<>();

        adapter = new JobOverviewAdapter(this,list,R.layout.job_overview_item);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.get(position).getPigai().equals("1")){
                    List<String> daiList = new ArrayList<>();
                    int fgtIndex = 0;
                    for (int i=0;i<list.size();i++){
                        if (list.get(i).getPigai().equals("1")){
                            daiList.add(list.get(i).getQuestion_id());
                        }
                    }

                    for (int i=0;i<daiList.size();i++){
                        if (daiList.get(i).contains(list.get(position).getQuestion_id())){
                            fgtIndex = i;
                        }
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString("id",work_id);
                    bundle.putString("class_id",class_id);
                    bundle.putInt("fgtindex",fgtIndex);
                    startActivity(TopicDetailsAty.class,bundle);
                }
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        teacher.workDetail(work_id,class_id,this,0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        teacher.workDetail(work_id,class_id,this,0);
    }

    @OnClick({R.id.iv_back,R.id.ll_submit_details})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_submit_details://提交详情
                Bundle bundle = new Bundle();
                bundle.putString("id",work_id);
                bundle.putString("class_id",class_id);
                startActivity(SubmitDatailsAty.class,bundle);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                jobOverviewBean = AppJsonUtil.getObject(result,JobOverviewBean.class);
                list.clear();
                list.addAll(jobOverviewBean.getAnswer());
                adapter.notifyDataSetChanged();
                tvSubjectNum.setText("题目正确率（"+list.size()+"题）");
                tvWorkTitle.setText(jobOverviewBean.getTitle());
                tvFinishTime.setText(jobOverviewBean.getCut_time());
                tvRightPercent.setText(jobOverviewBean.getRight_percent());
                tvFinish.setText(jobOverviewBean.getWancheng());
                tvSaveNum.setText(jobOverviewBean.getNum()+"/"+jobOverviewBean.getTijiao());
                break;
        }
        super.onSuccess(result, call, response, what);
    }
}
